<?php

namespace app\controller;

use app\BaseController;
use app\validate\FaceVotelog;
use think\facade\Request;
use think\exception\ValidateException;
use think\facade\Db;

class Index extends BaseController
{
    public function index()
    {

        if (Request::has('wx_openid', 'post') && Request::has('zone', 'post')) {
            $wx_openid = Request::param("wx_openid");
            $zone = Request::param("zone");
            try {
                validate(FaceVotelog::class)->check(
                    [
                        'wx_openid' => $wx_openid,
                        'zone' => $zone,
                    ]
                );
            } catch (ValidateException $e) {
                dump($e->getError());
                return;
            }
            $hot = Db::table('face_votelist')->where('zone', $zone)->value('hot');
            if ($hot === null) {
                return 'hot_fail';
            } else {
                $hot += 1;
                Db::name('face_votelist')->where('zone', $zone)->update(['hot' => $hot]);
                $face_votelog = ['wx_openid' => $wx_openid, 'zone' => $zone,'create_time' => date('Y-m-d H:i:s', time())];
                Db::name('face_votelog')->save($face_votelog);
                return 'success';
            }

        }
        return 'fail';
    }

    public function hello($name = 'ThinkPHP6')
    {
        return 'hello,' . $name;
    }
}
